public class Rational{
    int fenzi=1;
    int fenmu=1;

    void setFenzi(int zi)
    {
        int c = f(Math.abs(zi),fenmu);//最大公约数
        fenzi =  zi / c;
        fenmu = fenmu /c;
        if(fenzi<0&&fenmu<0)
        {
            fenzi=-fenzi;
            fenmu=-fenmu;
        }

    }
    void setFenmu(int mu)
    {
        int c = f(fenzi,Math.abs(mu));//最大公约数
        fenzi = fenzi /c;
        fenmu = mu/c;
        if(fenzi<0&&fenmu<0)
        {
            fenzi=-fenzi;
            fenmu=-fenmu;
        }

    }


    int f(int a,int b) //求最大公约数
    {
        int g=0,i=0;
        if(a==0)
            return 1;
        if(b%a==0)
        {
            return a;
        }
        else
        {

            for(i=a;i>=1;i--)
            {
                if(b%i==0 && a%i==0)
                {
                    g=i;
                    break;
                }
            }
        }
        return g;

    }
    int getFenzi()
    {
        return fenzi;
    }
    int getFenmu()
    {
        return fenmu;
    }
    Rational add(Rational r) //加
    {
        int a = r.fenzi;
        int b = r.fenmu;
        int newfenzi = fenzi*b + a*fenmu;
        int newfenmu = fenmu * b;
        Rational result = new Rational();
        result.setFenzi(newfenzi);
        result.setFenmu(newfenmu);
        return result;
    }
    Rational sub(Rational r) //减
    {
        int a = r.fenzi;
        int b = r.fenmu;
        int newfenzi = fenzi*b - a*fenmu;
        int newfenmu = fenmu*b;
        Rational result = new Rational();
        result.setFenzi(newfenzi);
        result.setFenmu(newfenmu);
        return result;
    }
    Rational muti(Rational r) //乘
    {
        int a = r.fenzi;
        int b = r.fenmu;
        int newfenzi = a*fenzi;
        int newfenmu = fenmu*b;
        Rational result = new Rational();
        result.setFenzi(newfenzi);
        result.setFenmu(newfenmu);
        return result;
    }
    Rational div(Rational r) //除
    {
        int a = r.fenzi;
        int b = r.fenmu;
        int newfenzi = fenzi*b;
        int newfenmu = fenmu*a;
        Rational result = new Rational();
        result.setFenzi(newfenzi);
        result.setFenmu(newfenmu);
        return result;
    }
}